package com.crane.app.web.controller;

import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.utils.BaseContextHandler;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.app.bean.req.AppBindingStudentReq;
import com.crane.oss.app.bean.req.AppUnBindingStudentReq;
import com.crane.oss.app.bean.req.AppUpdatePicReq;
import com.crane.oss.app.bean.request.appuser.AppLoginCodeRequest;
import com.crane.oss.app.bean.request.appuser.AppLoginRequest;
import com.crane.oss.app.bean.request.appuser.ForgetPasswordRequest;
import com.crane.oss.app.bean.request.appuser.WxLoginParam;
import com.crane.oss.app.bean.resp.AppClassesInfoResp;
import com.crane.oss.app.bean.resp.AppUserInfoResp;
import com.crane.oss.app.bean.resp.AppUserStudentInfoResp;
import com.crane.oss.app.service.AppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月20日 23时05分57秒
 */

@Api(value = "小程序接口", tags = {"小程序接口"})
@RestController
@RequestMapping("/app/appUser")
public class AppUserController {


  @Autowired
  AppUserService appUserService;


  @ApiOperation(value = "微信小程序登录", notes = "微信小程序登录")
  @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "WxLoginParam")
  @PostMapping("/weChatLogin")
  public SingleResponse<AppUserInfoResp> weChatLogin(@RequestBody @Valid WxLoginParam request) {
    AppUserInfoResp appLoginRequest = appUserService.weChatLogin(request);
    return SingleResponse.of(appLoginRequest);
  }

  @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
  @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppLoginCodeRequest")
  @PostMapping("/loginCode")
  public SingleResponse<String> loginCode(@RequestBody @Valid AppLoginCodeRequest request) {
    String phone = appUserService.loginCode(request);
    return SingleResponse.of(phone);
  }

  @ApiOperation(value = "切换身份", notes = "切换身份")
  @ApiImplicitParam(name = "userType", value = "切换后的类型，1老师2家长", required = true)
  @PostMapping("/updateUserType")
  public Response updateUserType(@RequestBody Integer userType) {
    appUserService.updateUserType(userType);
    return Response.buildSuccess();
  }

  @ApiOperation(value = "切换学生", notes = "切换学生")
  @ApiImplicitParam(name = "userType", value = "切换学生ID", required = true)
  @PostMapping("/updateStudentId")
  public Response updateStudentId(@RequestBody Long studentId) {
    appUserService.updateStudentId(studentId);
    return Response.buildSuccess();
  }

  @ApiOperation(value = "编辑头像", notes = "编辑头像")
  @PostMapping("/updateUserPicUrl")
  public Response updateUserPicUrl(@RequestBody AppUpdatePicReq appUpdatePicReq) {
    appUserService.updateUserPicUrl(appUpdatePicReq);
    return Response.buildSuccess();
  }

  @ApiOperation(value = "根据班级Id查找班级信息", notes = "根据班级Id查找本级信息")
  @PostMapping("/getClassInfo")
  @ApiImplicitParam(name = "classId", value = "班级Id", required = true)
  public SingleResponse<AppClassesInfoResp> getClassInfo(@RequestBody Long classId) {
    AppClassesInfoResp appUpdatePicReq =appUserService.getClassInfo(classId);
    return SingleResponse.of(appUpdatePicReq);
  }
  @ApiOperation(value = "绑定学生", notes = "绑定学生")
  @PostMapping("/bindingStudent")
  public Response bindingStudent(@RequestBody AppBindingStudentReq appBindingStudentReq) {
     appUserService.bindingStudent(appBindingStudentReq);
    return Response.buildSuccess();
  }

  @ApiOperation(value = "解绑学生", notes = "解绑学生")
  @PostMapping("/unboundStudent")
  public Response unboundStudent(@RequestBody AppUnBindingStudentReq appBindingStudentReq) {
    appUserService.unboundStudent(appBindingStudentReq);
    return Response.buildSuccess();
  }
  /**
   * 用户密码登录
   */
  @ApiOperation(value = "用户登录", notes = "用户登录接口", httpMethod = "POST")
  @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppLoginRequest")
  @ResponseBody
  @PostMapping(value = "/login")
  public SingleResponse<AppUserInfoResp> login(@RequestBody @Valid AppLoginRequest request) {
    AppUserInfoResp appUserInfoResp = appUserService.login(request);
    return SingleResponse.of(appUserInfoResp);
  }

  @ApiOperation(value = "根据token获取用户的详情信息和绑定学生信息", notes = "根据token获取用户的详情信息和绑定学生信息")
  @GetMapping("/getUserInfo")
  public SingleResponse<AppUserStudentInfoResp> getUserInfo() {
    AppUserStudentInfoResp userInfoResp = appUserService.queryStudentInfo(BaseContextHandler.getUserId());
    return SingleResponse.of(userInfoResp);
  }


  @ApiOperation(value = "忘记密码", notes = "忘记密码")
  @PostMapping("/updatePassword")
  public SingleResponse<AppUserInfoResp> forgetPassword(
      @RequestBody @Valid ForgetPasswordRequest request) {
    appUserService.forgetPassword(request);
    // 校验用户是否存在
    AppUserInfoResp appUserInfoResp = new AppUserInfoResp();
    return SingleResponse.of(appUserInfoResp);
  }


  @ApiOperation(value = "退出", notes = "退出")
  @GetMapping("/loginOut")
  public Response loginOut() {
    appUserService.loginOut();
    return Response.buildSuccess();
  }
}
